#########################################################################################
# Title: Figure4.R
# Author: Yuki Atsusaka (atsusaka@rice.edu)
# Aim: Code to replicate Figure 4 in Atsusaka (2021)
#########################################################################################

# CLEAN THE GLOBAL ENVIRONMENT AND READ PACKAGES
rm(list=ls())
library(plotly)
library(tidyverse)
library(RColorBrewer)
require("grDevices")

##############################################################
# COMPUTING MODLE PREDICTIONS AND READING OBSERVED DATA
##############################################################

# COMPUTING MODEL PREDICTIONS
M_vec <- seq(from=0, to=100, by=1)     # Vector of M: Racial margin of victory
C_vec <- seq(from=0, to=100, by=1)     # Vector of C: % minority voters
Mt <- matrix(NA, ncol=101, nrow=101)   # Matrix for storing model predictions
                                       # row = C, column = M
for(j in seq_along(M_vec)){  # Loop over M
 M <- M_vec[j]

for(i in seq_along(C_vec)){  # Loop over C
 C <- C_vec[i]   
 q <- sqrt(M*C) - 50

Mt[i,j] <- pnorm(q=q, mean=0, sd=1)    # Model prediction for (i,j)-th cell

}
}

# READING OBSERVED MINORITY CANDIDATE EMERGENCE
dt <- read_csv(here::here("Data_StateLegislative.csv")) %>%
      filter(unusual==0) # Dropping unusual cases

##############################################################
# VISUALIZING MODLE PREDICTIONS
##############################################################

filled.contour(x=M_vec, y=C_vec, z=Mt, 
               xlim=c(-4,104), ylim=c(0,100),
               plot.axes =
                 {text(dt$M[dt$minority_win==0], dt$C[dt$minority_win==0], 
                       labels=dt$state.lower[dt$minority_win==0], cex=1)
                  text(dt$M[dt$minority_win==1], dt$C[dt$minority_win==1], 
                       labels=dt$state[dt$minority_win==1], cex=1, font=1)
                  text(16, 95, labels=expression(widehat(P)[win] < 0.1), font=2, cex=1.2, col="white")
                  text(38, 95, labels=expression(widehat(P)[win] > 0.9), font=2, cex=1.2, col="white")
                  text(75,8, labels="Minority Winner", font=2, cex=1.2, col="white")
                  text(78,2.8, labels="No Minority Winner", font=2, cex=1.2,col="white")
                  text(52,8, labels="STATE:", font=1, cex=1.2, col="white")
                  text(53.6,3, labels="state:", font=1, cex=1.2, col="white")
                  axis(1, seq(0, 100, by = 25))
                  axis(2, seq(0, 100, by = 20))},
               col=rev(scales::alpha(brewer.pal(n=10, name="Spectral"),0.75)), nlevels=10,
               plot.title = title(xlab="M (Racial Margin of Victory)", 
                                  ylab="C (% Minority Voters)",cex.lab=1.3))

##################################################################
# Manually save the image as a PDF file with width=8 and height=7
##################################################################

#########################################################################################
# END OF THIS R SOURCE FILE
#########################################################################################